import 'package:flutter/material.dart';
import 'package:badges/badges.dart' as badges;
import 'package:lx_widgets_library/const/color.dart';

// AnimatedBottomNavigationBar 是一个自定义的底部导航栏，支持动画效果和数字标记
// 以及小红点标记。它允许用户在多个选项之间进行选择，并提供了丰富的自定义选项。
class AnimatedBottomNavigationBar extends StatefulWidget {
  // 当前选中项索引
  final int currentIndex;
  // 底部导航栏项
  final List<BottomNavigationBarItem> items;
  // 背景色
  final Color? backgroundColor;
  // 点击事件
  final ValueChanged<int> onTap;
  // 选中项颜色
  final Color selectedTextColor;
  // 选中项文字样式
  final TextStyle? selectedTextStyle;
  // 未选中项颜色
  final Color unselectedTextColor;
  // 未选中项文字样式
  final TextStyle? unselectedTextStyle;
  // 图标大小
  final double iconSize;
  // 阴影
  final double elevation;
  // 动画时长
  final Duration animationDuration;
  // 动画曲线
  final Curve animationCurve;
  // 数字标记
  final List<int>? badgeCounts;
  // 是否显示小红点
  final List<bool>? showBadges;
  // 小红点颜色
  final Color badgeColor;
  // 小红点文字颜色
  final Color badgeTextColor;

  const AnimatedBottomNavigationBar({
    super.key,
    required this.currentIndex,
    required this.items,
    required this.onTap,
    this.selectedTextColor = Colors.blue,
    this.selectedTextStyle,
    this.unselectedTextColor = Colors.grey,
    this.unselectedTextStyle,
    this.iconSize = 24.0,
    this.elevation = 8.0,
    this.animationDuration = const Duration(milliseconds: 300),
    this.animationCurve = Curves.easeInOut,
    this.badgeCounts,
    this.showBadges,
    this.badgeColor = Colors.red,
    this.badgeTextColor = Colors.white,
    this.backgroundColor,
  })  : assert(badgeCounts == null || badgeCounts.length == items.length),
        assert(showBadges == null || showBadges.length == items.length);

  @override
  _AnimatedBottomNavigationBarState createState() =>
      _AnimatedBottomNavigationBarState();
}

class _AnimatedBottomNavigationBarState
    extends State<AnimatedBottomNavigationBar> {
  @override
  Widget build(BuildContext context) {
    return Theme(
      data: Theme.of(context).copyWith(
        splashColor: GrayColor.gray3.withOpacity(0.1),
        highlightColor: Colors.transparent,
        hoverColor: Colors.transparent,
      ),
      child: BottomNavigationBar(
        backgroundColor: widget.backgroundColor,
        currentIndex: widget.currentIndex,
        items: _buildItemsWithBadges(),
        onTap: widget.onTap,
        selectedItemColor: widget.selectedTextColor,
        selectedLabelStyle: widget.selectedTextStyle,
        unselectedItemColor: widget.unselectedTextColor,
        unselectedLabelStyle: widget.unselectedTextStyle,
        iconSize: widget.iconSize,
        elevation: widget.elevation,
        type: BottomNavigationBarType.fixed,
      ),
    );
  }

  List<BottomNavigationBarItem> _buildItemsWithBadges() {
    return widget.items.asMap().entries.map((entry) {
      final index = entry.key;
      final item = entry.value;

      Widget icon = _buildAnimatedIcon(item.activeIcon, item.icon, index);

      // 添加Badge
      if ((widget.badgeCounts != null && widget.badgeCounts![index] > 0) ||
          (widget.showBadges != null && widget.showBadges![index])) {
        icon = _buildBadge(icon, index);
      }

      return BottomNavigationBarItem(
        icon: icon,
        label: item.label,
        tooltip: item.tooltip,
      );
    }).toList();
  }

  Widget _buildBadge(Widget icon, int index) {
    if (widget.badgeCounts != null && widget.badgeCounts![index] > 0) {
      // 数字标记
      return badges.Badge(
        badgeContent: Text(
          widget.badgeCounts![index].toString(),
          style: TextStyle(
            color: widget.badgeTextColor,
            fontSize: 10,
          ),
        ),
        badgeColor: widget.badgeColor,
        padding: const EdgeInsets.all(4),
        borderRadius: BorderRadius.circular(10),
        position: badges.BadgePosition.topEnd(top: -12, end: -12),
        child: icon,
      );
    } else if (widget.showBadges != null && widget.showBadges![index]) {
      // 小红点
      return badges.Badge(
        badgeColor: widget.badgeColor,
        padding: const EdgeInsets.all(5),
        borderRadius: BorderRadius.circular(10),
        position: badges.BadgePosition.topEnd(top: -8, end: -8),
        child: icon,
      );
    }
    return icon;
  }

  Widget _buildAnimatedIcon(Widget activeIcon, Widget icon, int index) {
    bool isSelected = index == widget.currentIndex;
    return AnimatedContainer(
      duration: widget.animationDuration,
      curve: widget.animationCurve,
      transformAlignment: Alignment.center,
      transform: Matrix4.identity()
        ..scale(isSelected ? 1.2 : 1.0, isSelected ? 1.2 : 1.0),
      child: isSelected ? activeIcon : icon,
    );
  }
}
